import { _decorator, AudioSource, Component, director, Label, Node, Scene } from 'cc';
import { onTouch } from './onTouch'
const { ccclass, property } = _decorator;
@ccclass('backHouse')
export class backHouse extends Component {
    @property(Node)
    pause: Node = null;
    @property(Node)
    stopGame: Node = null;



    private isBgmPlaying: boolean = true;

    @property(Node)
    cows: Node = null;
    @property(Node)
    JIFEN: Node = null;
    @property(Node)
    JIFENZHANSHI: Node = null;
    @property(Node)
    horses: Node = null;

    @property(Label)
    timerLabel: Label = null;

    private totalTimeInSeconds: number = 20; // 倒计时总时间，以秒为单位
    private currentTimeInSeconds: number = this.totalTimeInSeconds; // 当前剩余时间，以秒为单位
    private timerInterval = null; // setInterval 的 ID
    private isPaused: boolean = false; // 增加一个标志来表示是否已暂停
    jifen = 0;
    onTouch = new onTouch()

    start() {
        console.log('游戏场景中user_name = ', globalThis._userInfo.user_name);

        // 播放BGM
        this.startCountdown();


    }

    // 开始倒计时
    startCountdown() {
        this.timerInterval = setInterval(() => {
            this.updateTimer();
        }, 1000); // 每秒更新一次

        this.updateTimer();
    }

    // 更新倒计时
    updateTimer() {
        if (this.currentTimeInSeconds > 0) {
            this.currentTimeInSeconds -= 1;
        } else {
            // 倒计时结束
            clearInterval(this.timerInterval);
            this.gameOver()
        }
        // 将剩余时间显示在Label上
        this.timerLabel.string = this.formatTime(this.currentTimeInSeconds);
    }
    formatTime(seconds: number): string {
        // 格式化剩余时间为分:秒的形式
        const minutes = Math.floor(seconds / 60);
        const remainingSeconds = seconds % 60;
        return `${remainingSeconds < 10 ? '0' : ''}${remainingSeconds}s`;
    }
    // 暂停计时
    pauseCountdown() {
        clearInterval(this.timerInterval);
        this.isPaused = true;
    }
    // 继续计时
    resumeCountdown() {
        if (!this.isPaused) {
            return;
        }
        this.isPaused = false;
        this.timerInterval = setInterval(() => {
            this.updateTimer();
        }, 1000);
        this.updateTimer();
    }
    // 清理计时器
    onDestroy() {
        if (this.timerInterval) {
            clearInterval(this.timerInterval);
        }
    }


    //游戏停止
    gameOver() {
        this.JIFENZHANSHI.getComponent(Label).string = '您的积分是:' + this.jifen;
        console.log('游戏停止');
        director.pause();
        this.stopGame.active = true;
        this.horses.active = false;


        this.updateItem();
        //将分数上传到服务器 并传回来

    }
    //游戏暂停
    interStop() {
        console.log('游戏暂停');
        director.pause();
        // this.horseAudio.pause();

        this.pause.active = true;
        this.horses.active = false;
        this.pauseCountdown()
    }

    //继续游戏按钮
    pauseGame() {
        director.resume();
        this.horses.active = true;
        this.pause.active = false;
        this.resumeCountdown();
    }
    //回到主页按钮
    backHouse() {
        this.stopGame.active = false;
        this.pause.active = false;

        director.pause();
        director.loadScene('scene3');
    }

    //重玩按钮
    pauseFal() {
        this.totalTimeInSeconds = 20;
        this.horses.active = true;
        console.log('游戏开始了');
        this.pauseGame()
        this.jifen = 0;
        this.JIFEN.getComponent(Label).string = '积分:' + 0;
        this.stopGame.active = false;
        console.log(this.totalTimeInSeconds);
        this.currentTimeInSeconds = this.totalTimeInSeconds;
        this.timerInterval = setInterval(() => {
            this.updateTimer();
        }, 1000);

    }


    setScore() {
        //积分加1
        // console.log('积分+++');

        this.jifen++;

        let label = this.JIFEN.getComponent(Label);
        label.string = '积分:' + this.jifen;
    }


    updateItem() {

        const jifen = this.jifen;

        if (jifen <= globalThis._userInfo.user_score) return;
        globalThis._userInfo.user_score = jifen;
        let db = wx.cloud.database()
        let userCollection = db.collection('user_info')
        userCollection.where({
            //先查询
            _id: globalThis._userInfo.user_id
        }).update({
            data: {
                user_score: globalThis._userInfo.user_score
            }
        }).then(res => {
            console.log('更新成功')

        }).catch(err => {
            console.log('更新失败', err)//失败提示错误信息
        });
    }

}


